{==============================================================================}
{  Acroprise Multi-Language Components                                         }
{                                                                              }
{  Copyright (C), 1999-2012, Acroprise Inc.                                    }
{  http://www.acroprise.com.tw                                                 }
{  ALL RIGHTS RESERVED                                                         }
{                                                                              }
{  Author   : Mustapha.Wang                                                    }
{  Version  : 4.1.15.2159                                                      }
{  Creation : 2002/05/06                                                       }
{  Revision :                                                                  }
{  Task     :                                                                  }
{==============================================================================}
unit acroMultiDDCatcher;

interface
{$I acroMultiLang.Inc}
uses
  Classes,SysUtils,
  acroMultiDD,acroMultiDDWriter,
  {$IFDEF UNICODE}
  acroCulture_Unicode,
  {$ELSE}
  acroCulture_Ansi,
  {$ENDIF}
  acroMultiClassMethod;

type
  TacroMultiDDKeyCatcher=class(TComponent)
  private
    FEvents:TacroMultiDDEvents;
    FDDFileName:string;
    FWriter:TTextCSVStreamDDWriter_Default;
    FActive:Boolean;
    FWriteComponentOwnerClassName:Boolean;
    FWriteComponentName:Boolean;
    procedure _NotFoundDisplayValue(Sender:TObject;ADDKey,ALCID,ASeqNo:string;ALookupComeFrom:TLookupComeFromWhere=lwUI);
    procedure SetDDFileName(const Value: string);
  public
    constructor Create(AOwner:TComponent);override;
    destructor Destroy;override;
  published
    property Active:Boolean read FActive write FActive;
    property DDFileName:string read FDDFileName write SetDDFileName;
    property WriteComponentOwnerClassName:Boolean read FWriteComponentOwnerClassName write FWriteComponentOwnerClassName default True;
    property WriteComponentName:Boolean read FWriteComponentName write FWriteComponentName;
  end;

implementation

{ TacroMultiDDKeyCatcher }

constructor TacroMultiDDKeyCatcher.Create(AOwner: TComponent);
begin
  inherited;
  FEvents:=TacroMultiDDEvents.Create(nil);
  FEvents.OnNotFoundDisplayValue:=_NotFoundDisplayValue;
  FWriteComponentOwnerClassName:=true;
end;

destructor TacroMultiDDKeyCatcher.Destroy;
begin
  FEvents.Free;
  if FWriter<>nil then FreeAndNil(FWriter);
  inherited;
end;

procedure TacroMultiDDKeyCatcher.SetDDFileName(const Value: string);
begin
  if FDDFileName<>Value then
  begin
    if FWriter<>nil then FreeAndNil(FWriter);
    FDDFileName := Value;
  end;
end;

procedure TacroMultiDDKeyCatcher._NotFoundDisplayValue(Sender: TObject; ADDKey,
  ALCID, ASeqNo: string;ALookupComeFrom:TLookupComeFromWhere=lwUI);
var
  vDDKey,vCatalogID:string;
begin
  if csDesigning in ComponentState then exit;

  if not FActive then exit;
  if FWriter=nil then
    if FDDFileName<>'' then
      FWriter:=TTextCSVStreamDDWriter_Default.Create(FDDFileName)
    else exit;

  if ALookupComeFrom=lwResourceString then
  begin
    vDDKey:='ResourceString';
    vCatalogID:=vDDKey;
    FWriter.WriteRecord(vDDKey,LCID_ENG,ASeqNo,vDDKey,'');
  end
  else
  begin
    if gCurrentReplacingComponent<>nil then
    begin
      vCatalogID:='';
      if FWriteComponentOwnerClassName and
         (gCurrentReplacingComponent.Owner<>nil) then
      begin
        vDDKey:=gCurrentReplacingComponent.Owner.ClassName;
        vCatalogID:=vDDKey;
        FWriter.WriteRecord(vDDKey,LCID_ENG,ASeqNo,vDDKey,'');
      end;
      if FWriteComponentName then
      begin
        if gCurrentReplacingComponent.Owner<>nil then
        begin
          vDDKey:=gCurrentReplacingComponent.Owner.ClassName+'.'+
            gCurrentReplacingComponent.Name+':'+
            gCurrentReplacingComponent.ClassName;
          vCatalogID:=gCurrentReplacingComponent.Owner.ClassName;
          FWriter.WriteRecord(vDDKey,LCID_ENG,ASeqNo,vDDKey,vCatalogID);
          vCatalogID:=vDDKey;
        end
        else
        begin
          vDDKey:=gCurrentReplacingComponent.ClassName;
          vCatalogID:=vDDKey;
          FWriter.WriteRecord(vDDKey,LCID_ENG,ASeqNo,vDDKey,'');
        end;
      end;
    end
    else
      vCatalogID:='';
  end;
  if ADDKey<>'' then
    FWriter.WriteRecord(ADDKey,LCID_ENG,ASeqNo,ADDKey,vCatalogID);
end;

end.
